package postfix;
import postfix.analysis.*;
import postfix.node.*;
public class Calculatrice extends DepthFirstAdapter {
	private Start startNode;
	
	public void inStart(Start node)
	{
		startNode = node;
	}
	
	public void outStart(Start node)
	{
		System.out.println(startNode.toString());
	}
	
	public void outAPlusExpr(APlusExpr node)
	{
		System.out.println(startNode.toString());
		
		AFactorExpr factorExpr;
		ATermFactor termFactor;
		ANumberTerm numberTerm;
		TNumber tNumber;
		
		int number1, number2, result;
		number1 = Integer.parseInt(node.getExpr().toString().trim());
		number2 = Integer.parseInt(node.getFactor().toString().trim());
		result = number1 + number2;
		
		tNumber = new TNumber(String.valueOf(result));
		numberTerm = new ANumberTerm();
		numberTerm.setNumber(tNumber);
		termFactor = new ATermFactor();
		termFactor.setTerm(numberTerm);
		factorExpr = new AFactorExpr();
		factorExpr.setFactor(termFactor);
		
		node.replaceBy(factorExpr);
	}
	
	public void outAExprTerm(AExprTerm node)
	{
		ANumberTerm numberTerm;
		TNumber tNumber;
		
		String result;
		result = node.getExpr().toString().trim();
		
		tNumber = new TNumber(result);
		numberTerm = new ANumberTerm();
		numberTerm.setNumber(tNumber);
		
		node.replaceBy(numberTerm);	
	}
	
	public void outAMinusExpr(AMinusExpr node)
	{
		System.out.println(startNode.toString());
		
		AFactorExpr factorExpr;
		ATermFactor termFactor;
		ANumberTerm numberTerm;
		TNumber tNumber;
		
		int number1, number2, result;
		number1 = Integer.parseInt(node.getExpr().toString().trim());
		number2 = Integer.parseInt(node.getFactor().toString().trim());
		result = number1 - number2;
		
		tNumber = new TNumber(String.valueOf(result));
		numberTerm = new ANumberTerm();
		numberTerm.setNumber(tNumber);
		termFactor = new ATermFactor();
		termFactor.setTerm(numberTerm);
		factorExpr = new AFactorExpr();
		factorExpr.setFactor(termFactor);
		
		node.replaceBy(factorExpr);
	}
	
	public void outAMultFactor(AMultFactor node)
	{
		System.out.println(startNode.toString());
		
		ATermFactor termFactor;
		ANumberTerm numberTerm;
		TNumber tNumber;
		
		int number1, number2, result;
		number1 = Integer.parseInt(node.getFactor().toString().trim());
		number2 = Integer.parseInt(node.getTerm().toString().trim());
		result = number1 * number2;
		
		tNumber = new TNumber(String.valueOf(result));
		numberTerm = new ANumberTerm();
		numberTerm.setNumber(tNumber);
		termFactor = new ATermFactor();
		termFactor.setTerm(numberTerm);
		
		node.replaceBy(termFactor);
	}
	
	public void outADivFactor(ADivFactor node)
	{
		System.out.println(startNode.toString());
		
		ATermFactor termFactor;
		ANumberTerm numberTerm;
		TNumber tNumber;
		
		int number1, number2, result;
		number1 = Integer.parseInt(node.getFactor().toString().trim());
		number2 = Integer.parseInt(node.getTerm().toString().trim());
		result = number1 / number2;
		
		tNumber = new TNumber(String.valueOf(result));
		numberTerm = new ANumberTerm();
		numberTerm.setNumber(tNumber);
		termFactor = new ATermFactor();
		termFactor.setTerm(numberTerm);
		
		node.replaceBy(termFactor);
	}
	
	public void outAModFactor(AModFactor node)
	{
		System.out.println(startNode.toString());
		
		ATermFactor termFactor;
		ANumberTerm numberTerm;
		TNumber tNumber;
		
		int number1, number2, result;
		number1 = Integer.parseInt(node.getFactor().toString().trim());
		number2 = Integer.parseInt(node.getTerm().toString().trim());
		result = number1 % number2;
		
		tNumber = new TNumber(String.valueOf(result));
		numberTerm = new ANumberTerm();
		numberTerm.setNumber(tNumber);
		termFactor = new ATermFactor();
		termFactor.setTerm(numberTerm);
		
		node.replaceBy(termFactor);
	}
}
